namespace Xmef.Engine
{
    using System;
    using Xmef.Interfaces;
    using Xmef.Library;

    [Serializable]
    public class PluginServiceProvider<T> : PluginIdentity, IPluginServiceProvider<T>, IPluginIdentity where T: class, IPluginServiceProvider<T>
    {
        private PluginIdentity _id = new PluginIdentity();

        int IPluginIdentity.Id
        {
            get
            {
                return _id.Id;
            }
        }

        private static IPluginService<T> _service;

        static PluginServiceProvider()
        {
            PluginServiceProvider<T>._service = new PluginService<T>();
        }

        protected PluginServiceProvider()
        {
        }

        public virtual void Finalize(IPluginServiceManager manager)
        {
            PluginServiceProvider<T>._service.Unregister(manager, this as T);
        }

        public virtual void Initialize(IPluginServiceManager manager)
        {
            PluginServiceProvider<T>._service.Register(manager, this as T);
        }

        public virtual void Register(IPluginServiceManager control)
        {
            PluginServiceManager.RegisterService<T>(control, PluginServiceProvider<T>._service);
        }

        public virtual void Unregister(IPluginServiceManager control)
        {
            PluginServiceManager.UnregisterService<T>(control, PluginServiceProvider<T>._service);
        }

        public IPluginService<T> PluginService
        {
            get
            {
                return PluginServiceProvider<T>._service;
            }
        }
    }
}

